package com.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.human.entity.Emp;
import com.human.dao.EmpMapper;
import com.human.entity.ResultVo;
import com.human.service.IEmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jzq
 * @since 2023-02-14
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {

    @Resource
    EmpMapper empMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public ResultVo<String> update(Emp emp) {
        String encode = passwordEncoder.encode(emp.getPassword());
        emp.setPassword(encode);
        empMapper.updateById(emp);
        return new ResultVo<>(true, "员工信息修改成功");
    }

    @Override
    public ResultVo<String> saveEmp(Emp emp) {
        String encode = passwordEncoder.encode(emp.getPassword());
        emp.setPassword(encode);
        emp.setIsAdmin(0);
        emp.setEdate(LocalDate.now());
        emp.setConversionTime(LocalDate.now().plusMonths(3L));
        empMapper.insert(emp);
        return new ResultVo<>(true, "新增员工成功", emp);
    }

    @Override
    public ResultVo<Page> page(int current, int pageSize, String name) {
        //分页构造器
        Page pageInfo = new Page(current, pageSize);

        //执行查询
        IPage empAndDept = empMapper.getEmpAndDept(pageInfo, name);
        return new ResultVo<Page>(true, "查询成功", empAndDept);
    }

    @Override
    public ResultVo<List> queryEmpByName(String name) {
        List<Emp> empList = empMapper.queryEmpByName(name);
        return new ResultVo<>(true, "查询成功", empList);
    }

    @Override
    public ResultVo<Emp> getEmpById(Integer id) {
        Emp emp = empMapper.getEmpById(id);
        if (emp != null) {
            return new ResultVo(true, "查询成功", emp);
        }
        return new ResultVo(false, "用户不存在");
    }

    @Override
    public ResultVo getAllEmp() {
        List<Emp> empList = empMapper.selectList(null);
        return new ResultVo(true, "查询成功", empList);
    }

    @Override
    public ResultVo<Emp> login(Emp emp, HttpServletRequest request) {
        //根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Emp::getUserName, emp.getUserName());
        Emp one = empMapper.selectOne(queryWrapper);

        //如果没有查询到则返回登录失败
        if (one == null) {
            return new ResultVo<>(false, "用户不存在");
        }

        //密码比对，如果不一致则返回登录失败
        if (!passwordEncoder.matches(emp.getPassword(), one.getPassword())) {
            return new ResultVo<>(false, "密码错误");
        }

        //查看员工状态
        if (one.getStatus() == 0) {
            return new ResultVo<>(false, "账号已禁用");
        }

        request.getSession().setAttribute("employee", one.getUserName());
        return new ResultVo<>(true, "登录成功", one);
    }


    @Override
    public ResultVo delEmp(Integer id) {
        int i = empMapper.deleteById(id);
        if (i < 0) {
            return new ResultVo(false, "删除失败");
        }
        return new ResultVo(true, "删除成功");
    }

    @Override
    public ResultVo getRestNum() {
        Integer restNum = empMapper.getRestNum();
        return new ResultVo(true, "查询成功", restNum);
    }

    @Override
    public ResultVo getWorkNum() {
        Integer workNum = empMapper.getWorkNum();
        return new ResultVo(true, "查询成功", workNum);
    }

    @Override
    public ResultVo getLeaveNum() {
        Integer leaveNum = empMapper.getLeaveNum();
        return new ResultVo(true, "查询成功", leaveNum);
    }
}
